﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoServices.Example.Models.DomainModels;
using MongoServices.Example.Repositories;
using MongoServices.Example.Utilities;

namespace MongoServices.Example.Services
{
	public class BlogPostService : ReadWriteService<BlogPost>, IBlogPostService
	{
		public BlogPostService(IBlogPostRepository repository)
			: base(repository)
		{

		}

		public void GenerateSamplePosts(int postCount)
		{
			IList<BlogPost> posts = new List<BlogPost>();

			for (var i = 0; i < postCount; i++)
			{
				var body = Ipsum.GetPhrase(1000);

				posts.Add(new BlogPost
					{
						Body = body,
						DatePosted = DateTime.Now.AddDays(-1 * i),
						Title = "Generated Post - " + DateTime.Now.AddDays(-1 * i).ToShortDateString()
					}
				);
			}

			Save(posts);
		}
		
		public static bool AfterSaveProcess(BlogPost post)
		{
			post.BodyShort = post.Body.Length > 400 ? post.Body.Substring(0, 400) : post.Body;

			return true;
		}
	}
}